package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-2-14 9:16
 */
public class 买卖股票的最佳时机_121 {
    public int maxProfit1(int[] prices) {
        int result = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            for (int j = i+1; j < prices.length; j++) {
                if (prices[i] < prices[j]) {
                    result = Math.max(result, prices[j] - prices[i]);
                }
            }
        }
        return result;
    }

    public int maxProfit(int[] prices) {
        int min = prices[0];
        int result = 0;
        for (int i = 1; i < prices.length; i++) {
            if (min > prices[i]) {
                min = prices[i];
            } else {
                result = Math.max(result, prices[i] - min);
            }
        }
        return result;
    }

    @Test
    public void testMaxProfit () {
        int[] prices = {7,1,5,3,6,4};
        System.out.println(maxProfit(prices));
    }
}
